<?php

/**
 * 
 * 分页类
 * 
 * @author LiXiang 李相  www.uuiso.com
 * 
 * 2011-10-16 17:24:35
 *
 */

class Page {
	// 总记录数 //by LiXiang
	private $totalNum;
	// 每页显示的条数 //by LiXiang
	private $pageNum;
	// 总页数 //by LiXiang
	private $totalPages;
	// 当前页 //by LiXiang
	private $currPage;
	// 页面起始记录 //by LiXiang
	private $pageFirst = 0;
	// 页面结束记录 //by LiXiang
	private $pageLast = 0;
	private $curPageRows; // 当前页条数
	// 上一页 //by LiXiang
	private $prePage;
	// 下一页 //by LiXiang
	private $nextPage;
	// 显示文字设置
	private $words = array('first'=>'第一页', 'prev'=>'上一页', 'next'=>'下一页', 'last'=>'最后一页', 'dot'=>'...');
	// HTML标签形式设置
	private $theme = '共 #curPageRows#/#totalNum# 条记录，当前 #currPage#/#totalPages# 页 #firstPage# #prevPage# #numLinks# #nextPage# #lastPage#';
	// 分页URL格式
	private $url = 'auto';
	// 分页URL参数名
	private $p = 'page';
	
	function __construct($totalNum, $pageNum = 30, $p = 'page'){
		$this->totalNum = intval($totalNum);
		$this->pageNum = intval($pageNum);
		$this->totalPages = ceil($totalNum/$pageNum);
		$this->p = !empty($p) && is_string($p) ? $p : 'page';
		if(isset($_GET[$this->p]) && is_numeric($_GET[$this->p]) && $_GET[$this->p] > 0){
			if($_GET[$this->p] > $this->totalPages)
			{
				$this->setCurrPage($this->totalPages);
			}
			else
			{
				$this->setCurrPage($_GET[$this->p]);
			}
		}else{
			$this->setCurrPage(1);
		}
	}

	public function setCurrPage($page){
		if(($page = intval($page)) <= $this->totalPages && $page > 0){
			$this->currPage = $page;
			$this->pageFirst = $page == 1 ? 0 : (($page -1) * $this->pageNum);
			$this->pageLast = $page == $this->totalPages ? $this->totalNum : $page * $this->pageNum;
			$this->curPageRows = $page >= $this->totalPages ? ($this->totalNum % $this->pageNum == 0 ? $this->pageNum : $this->totalNum % $this->pageNum) : $this->pageNum;
			$this->prePage = $page == 1 ? 1 : $page - 1;
			$this->nextPage = $page == $this->totalPages ? $this->totalPages : $page + 1;
		}
		else
		{
			$this->totalPages = 1;
			$this->currPage = 1;
			$this->pageFirst = 0;
			$this->pageLast = 0;
			$this->curPageRows = 0;
			$this->prePage = 0;
			$this->nextPage = 1;
		}
	}
	
	/**
	 * @return the $currPage
	 * by LiXiang
	 */
	public function getCurrPage() {
		return $this->currPage;
	}
	
	/**
	 * @param field_type $words
	 */
	public function setWords($words) {
		$this->words = $words;
	}
	
	public function setWord($key, $value) {
		if(isset($this->words[$key])){
			$this->words[$key] = $value;
		}
	}
	/**
	 * 
	 * 分页变量使用 #pageNum# 占位符 
	 * @param string $url
	 */
	public function setUrl($url){
		$this->url = $url;
	}
	

	/**
	 * @param field_type $theme
	 */
	public function setTheme($theme) {
		$this->theme = $theme;
	}

	/**
	 * @return the $prePage
	 * by LiXiang
	 */
	public function getPrePage() {
		return $this->prePage;
	}

	/**
	 * @return the $nextPage
	 * by LiXiang
	 */
	public function getNextPage() {
		return $this->nextPage;
	}

	/**
	 * @return the $pageFirst
	 * by LiXiang
	 */
	public function getPageFirst() {
		return $this->pageFirst;
	}

	/**
	 * @return the $pageLast
	 * by LiXiang
	 */
	public function getPageLast() {
		return $this->pageLast;
	}
	
	public function getCurPageRows()
	{
		return $this->curPageRows;
	}
	
	/**
	 * @return the $totalPages
	 * by LiXiang
	 */
	public function getTotalPages() {
		return $this->totalPages;
	}
	
	private function getUrl(){
		if($this->url == 'auto' || empty($this->url)){
			$urlParse = parse_url($_SERVER['REQUEST_URI']);
			$params = array();
			if(isset($urlParse['query'])){
				parse_str($urlParse['query'],$params);
			}
			$params[$this->p] = '#pageNum#';
			
			$this->url = $urlParse['path'].'?'.str_replace('%23', '#', http_build_query($params));
		}
		return $this->url;
	}
	
	public function getPageOptionData($showNum = 6)
	{
		//如果只有一页返回空
		if($this->totalPages < 2)
		{
			//return array();
		}
		$url = $this->getUrl();
		
		$first = '';
		$prev = '';
		if($this->currPage == 1){
			//$first = '';
			//$prev = '';
		}else{
			$first = str_replace('#pageNum#', '1', $url);
			$prev = str_replace('#pageNum#', $this->prePage, $url);
		}
		
		$numLiks = array();
		if($this->totalPages > --$showNum){
			// 如果当前页数减去（显示的页码数量处以2的最小值）大于1，那么开始页码数就是前者，否则开始页数为1 by LiXiang
			$startPage = $this->currPage - floor($showNum/2);
			$startPage = $startPage > 1 ? $startPage : 1;
			// 开始起始页码数加显示的页码数小于或等于总页数，那么开始页码数不变，否则开始页码数为总页码数减去显示的页码数量 by LiXiang
			$startPage = $startPage + $showNum < $this->totalPages ? $startPage : $this->totalPages - $showNum;
			// 如果当前页码数大于$showNum除以2的最大整数(当$showNum为偶数时需要加1)，表示前面的页码存在被隐藏 by LiXiang
			if($this->totalPages > $showNum + 1 && ($this->currPage > ($showNum%2 == 0 ? ceil($showNum/2) + 1 : ceil($showNum/2)))){
				$numLiks[] = array('page'=>'...', 'url'=>str_replace('#pageNum#', $startPage - 1, $url));
			}
			// 输出$showNum个页码号 by LiXiang
			$endPage = $showNum + $startPage;
			for($i = $startPage; $i <= $endPage; ++$i){
				if($this->currPage == $i){
					$numLiks[] = array('page'=>$i, 'url'=>'');
				}else{
					$numLiks[] = array('page'=>$i, 'url'=>str_replace('#pageNum#', $i, $url));
				}
			}
			// 如果当前页码数加上$showNum除以2的最大整数小于总页码数，表示后面的的页码存在被隐藏 by LiXiang
			if($this->totalPages > $showNum + 1 && ($this->currPage + ceil($showNum/2) < $this->totalPages)){
				$numLiks[] = array('page'=>'...', 'url'=>str_replace('#pageNum#', $endPage + 1, $url));
			}
		}else{
			for($i = 1; $i <= $this->totalPages; ++$i)
			{
				if($this->currPage == $i)
				{
					$numLiks[] = array('page'=>$i, 'url'=>'');
				}
				else
				{
					$numLiks[] = array('page'=>$i, 'url'=>str_replace('#pageNum#', $i, $url));
				}
			}
		}
		
		$next = '';
		$last = '';
		if($this->currPage == $this->totalPages)
		{
			//$next = '';
			//$last = '';
		}
		else
		{
			$next = str_replace('#pageNum#', $this->nextPage, $url);
			$last = str_replace('#pageNum#', $this->totalPages, $url);
		}
		
		return array('first'=>$first, 'prev'=>$prev, 'numLinks'=>$numLiks, 'next'=>$next, 'last'=>$last);
	}
	
	/**
	 * @param $showNum 需要显示页码列表的个数
	 */
	public function getHtml($showNum = 6){
		//如果只有一页返回空
		if($this->totalPages < 2)
		{
			//return '';
		}
		$url = $this->getUrl();
		
		$first = '';
		$prev = '';
		if($this->currPage == 1){
			//$first = '<span>'.$this->words['first'].'</span>';
			//$prev = '<span>'.$this->words['prev'].'</span>';
		}else{
			$first = str_replace('#pageNum#', '1', '<span><a href="'.$url.'">'.$this->words['first'].'</a></span>');
			$prev = str_replace('#pageNum#', $this->prePage, '<span><a href="'.$url.'">'.$this->words['prev'].'</a></span>');
		}
		
		$numLiks = '';
		if($this->totalPages > --$showNum){
			// 如果当前页数减去（显示的页码数量处以2的最小值）大于1，那么开始页码数就是前者，否则开始页数为1 by LiXiang
			$startPage = $this->currPage - floor($showNum/2);
			$startPage = $startPage > 1 ? $startPage : 1;
			// 开始起始页码数加显示的页码数小于或等于总页数，那么开始页码数不变，否则开始页码数为总页码数减去显示的页码数量 by LiXiang
			$startPage = $startPage + $showNum < $this->totalPages ? $startPage : $this->totalPages - $showNum;
			// 如果当前页码数大于$showNum除以2的最大整数(当$showNum为偶数时需要加1)，表示前面的页码存在被隐藏 by LiXiang
			if($this->totalPages > $showNum + 1 && ($this->currPage > ($showNum%2 == 0 ? ceil($showNum/2) + 1 : ceil($showNum/2)))){
				$numLiks .= '<span>&nbsp;'.$this->words['dot'].'&nbsp;</span>';
			}
			// 输出$showNum个页码号 by LiXiang
			$endPage = $showNum + $startPage;
			for($i = $startPage; $i <= $endPage; ++$i){
				if($this->currPage == $i){
					$numLiks .= '<span class="curr_page">&nbsp;'.$i.'&nbsp;</span>';
				}else{
					$numLiks .= str_replace('#pageNum#', $i, '<span>&nbsp;<a href="'.$url.'">'.$i.'</a>&nbsp;</span>');
				}
			}
			// 如果当前页码数加上$showNum除以2的最大整数小于总页码数，表示后面的的页码存在被隐藏 by LiXiang
			if($this->totalPages > $showNum + 1 && ($this->currPage + ceil($showNum/2) < $this->totalPages)){
				$numLiks .= '<span>&nbsp;'.$this->words['dot'].'&nbsp;</span>';
			}
		}else{
			for($i = 1; $i <= $this->totalPages; ++$i){
				if($this->currPage == $i){
					$numLiks .= '<span class="curr_page">&nbsp;'.$i.'&nbsp;</span>';
				}else{
					$numLiks .= str_replace('#pageNum#', $i, '<span>&nbsp;<a href="'.$url.'">'.$i.'</a>&nbsp;</span>');
				}
			}
		}
		
		$next = '';
		$last = '';
		if($this->currPage == $this->totalPages){
			//$next = '<span>'.$this->words['next'].'</span>';
			//$last = '<span>'.$this->words['last'].'</span>';
		}else{
			$next = str_replace('#pageNum#', $this->nextPage, '<span><a href="'.$url.'">'.$this->words['next'].'</a></span>');
			$last = str_replace('#pageNum#', $this->totalPages, '<span><a href="'.$url.'">'.$this->words['last'].'</a></span>');
		}
		
		$html = str_replace(
			array('#totalNum#', '#curPageRows#', '#currPage#', '#totalPages#', '#firstPage#', '#prevPage#', '#numLinks#', '#nextPage#', '#lastPage#'), 
			array($this->totalNum, $this->curPageRows, $this->currPage, $this->totalPages, $first, $prev, $numLiks, $next, $last),
			$this->theme
		);
		
		return $html;
	}

}
/*
 * 使用方式参考
 */

/*
header('Content-type:text/html;charset=utf-8');
$page = new Page(1200, 600, 'p');
$page->setTheme('总共 &nbsp;#totalNum# &nbsp;条信息，当前第&nbsp;#currPage#&nbsp;页&nbsp;/&nbsp;总共#totalPages#页 &nbsp;#firstPage# &nbsp;#prevPage# &nbsp;#numLinks# &nbsp;#nextPage# &nbsp;#lastPage#');
//$page->setUrl('/abc_index_#pageNum#.html');
echo $page->getHtml(10);

echo '<br />';
echo $page->getPageFirst();
echo '<br />';
echo $page->getCurPageRows();
echo '<br />';
echo $page->getPageLast();
echo '<br />-----<br />';
*/

?>